using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace BiztalkScriptingFunctoidExplorer
{
    public partial class MainWin : Form
    {

        Library lib = new Library();
        
        public MainWin()
        {
            InitializeComponent();
            dataGridView1.DataSource = lib.FunctoidInfoDS;
            dataGridView1.DataMember = "Functoids";
            label2.Text = "";
            linkLabel1.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                lib.FunctoidInfoDS.Clear();
                try
                {
                    FileInfo[] Maps = lib.GetMaps(textBox1.Text);

                    foreach (FileInfo fi in Maps)
                    {
                        lib.UpdateFunctoidData(fi,FilterDuplicates.Checked);
                    }
                    label2.Text = String.Format("{0} Maps found containing {1} functoid definitions", Maps.GetLength(0), lib.FunctoidInfoDS.Functoids.Count);
                    linkLabel1.Visible = true;
                }
                catch (System.Exception ex)
                {

                    MessageBox.Show(ex.Message);
                    linkLabel1.Visible = false;
                }

            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
                folderBrowserDialog1.ShowDialog();
                textBox1.Text = folderBrowserDialog1.SelectedPath;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //showdetail(e.RowIndex, e.ColumnIndex);
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            showdetail(e.RowIndex,e.ColumnIndex);
        }



        private void showdetail(int row, int column)
        {
            if (row >= 0 & column >= 0)
            {
                DataGridViewCell cell = (DataGridViewCell)dataGridView1.Rows[row].Cells[column];
                DetailWin frm2 = new DetailWin(cell.Value.ToString());
                frm2.ShowDialog();
            }
        }


        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right) showdetail(e.RowIndex, e.ColumnIndex);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            saveFileDialog1.FileName = "FunctoidExplorerDump.xml";
            saveFileDialog1.Filter = "xml file |*.xml";
            saveFileDialog1.InitialDirectory = @"c:\";
            saveFileDialog1.ShowDialog();
            lib.FunctoidInfoDS.WriteXml(saveFileDialog1.FileName,XmlWriteMode.IgnoreSchema);
        }

    }
}